8. Master the functions in Dart and you master the Force! |
您所在的位置:网站首页 › function assert param declared › 8. Master the functions in Dart and you master the Force! |
Directory Portals: A Guide to The Quick Start To Flutter As mentioned in the previous section, in Dart, a Function is also an object of type Function. It can be assigned to a variable and passed as an argument. Declare a functionThis is a normal function declaration: bool isNoble(int atomicNumber) { returnatomicNumber ! = null; }Copy the codeIf a function does not display a declared return value, Dart automatically deduces its return value type. void main() { bool b = isNoble(2); print(b); } isNoble(int atomicNumber) { returnatomicNumber ! = null; } true Copy the codeIf a function does not display a return, it will return NULL by default. fun(){} assert(fun() == null) Copy the codeIf a function has only one expression, use the shorthand: bool isNoble(int atomicNumber) = atomicNumber ! = null; isNoble(int atomicNumber) = atomicNumber ! = null;Copy the code Optional named parameterThe {} symbol can be used to specify the name of a function parameter. void enableFlags({bool bold = false, bool hidden = false, @required String content}) { // ... } Copy the codeAs you can see, Dart allows us to set default values for parameters. Call: enableFlags(bold: true, content: 'required'); enableFlags(bold: true, hidden: false, content: 'required'); Copy the codeUse the @required annotation argument to indicate the required argument, which you cannot omit when calling. This form of writing is also allowed and not used: void doStuff( {Listint list = const [1, 2, 3], MapString, String gifts = const { 'first': 'paper'.'second': 'cotton'.'third': 'leather'{}})print('list: $list'); print('gifts: $gifts'); } Copy the code Optional position parameterWith the [] symbol, you can specify that the arguments to the position are optional: String say(String from, String msg, [String device = 'carrier pigeon', String mood]) { var result = '$from says $msg'; if(device ! = null) { result ='$result with a $device'; } if(mood ! = null) { result ='$result (in a $mood mood)'; } return result; } assert(say('Bob'.'Howdy') = ='Bob says Howdy with a carrier pigeon'); Copy the codeThe parameters of device and mood are optional, and device has a default value. Function as variableAs mentioned earlier, a Function in Dart is an object of type Function, so it can be assigned to a variable. var say= (str){ print(str); }; say("hi world"); Copy the code Function as an input parameterFunction as an object, it can of course also be passed as a parameter: void execute(var callback){ callback(); } execute(()=print("xxx")) Copy the code A function closureThe Dart function has features that make it easy to implement closures: Function makeAdder(num addBy) {return (num i) = addBy + i; } void mainVar add2 = makeAdder(2); var add2 = makeAdder(2); Var add4 = makeAdder(4); assert(add2(3) == 5); assert(add4(3) == 7); }Copy the codeFrom this example, you can get a sense of the power of closures, which define a dynamically configurable function object. The typedef keywordDart provides the typedef keyword to define a function format (with strict format checking), such as: typedef int Compare(Object a, Object b); Copy the codeA function of this form is then defined as a Compare. It's actually a similar concept to the definition of a class. Consider this example: Compare test = (Object a, Object b){ return 0; } Copy the codeA function of this type is a function of type Compare. When you assign a function to a variable of Compare, it checks to see if the argument types and return types correspond exactly to Compare. If they do not, the compiler will report an error. Directory Portals: A Guide to The Quick Start To Flutter How can I be found?Portal:CoorChice homepage Portal:Lot of CoorChice |
今日新闻 |
推荐新闻 |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |